Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The pure-color npm package is a lightweight library for color manipulation and conversion. It provides a set of functions to work with colors in various formats, such as RGB, HSL, and HEX, and allows for operations like color blending, lightening, darkening, and more.
Color Conversion
Convert colors between different formats. In this example, HSL is converted to RGB.
const color = require('pure-color');
const rgb = color.convert.hsl.rgb([0.5, 0.5, 0.5]);
console.log(rgb); // [63.75, 191.25, 191.25]
Color Blending
Blend two colors together. This example blends red and blue in equal parts to produce a purple color.
const color = require('pure-color');
const blendedColor = color.blend.rgb([255, 0, 0], [0, 0, 255], 0.5);
console.log(blendedColor); // [127.5, 0, 127.5]
Color Lightening
Lighten a color by a given percentage. This example lightens a gray color by 20%.
const color = require('pure-color');
const lightenedColor = color.manipulate.lighten([100, 100, 100], 0.2);
console.log(lightenedColor); // [120, 120, 120]
Color Darkening
Darken a color by a given percentage. This example darkens a gray color by 20%.
const color = require('pure-color');
const darkenedColor = color.manipulate.darken([100, 100, 100], 0.2);
console.log(darkenedColor); // [80, 80, 80]
Chroma.js is a powerful and versatile color manipulation library that supports a wide range of color spaces and provides advanced color manipulation functions. It is more feature-rich compared to pure-color, offering additional functionalities like color scales and color interpolation.
The color package is a comprehensive library for color conversion and manipulation. It supports a variety of color spaces and provides chainable methods for color transformations. It is similar to pure-color but offers a more extensive API and better integration with other libraries.
TinyColor is a small, fast library for color manipulation and conversion. It provides a simple API for common color operations and is designed to be lightweight and easy to use. It is similar to pure-color in terms of functionality but focuses on simplicity and performance.
pure-color
is a color conversion and parsing library for the browser and node. It offers conversions between rgb
, hsl
, hsv
, hwb
, cmyk
, xyz
, lab
, lch
, hex
. It offers parsing of rgb(a)
, hex
and hsl(a)
strings.
Install with npm:
npm install pure-color --save
The library is structured to allow requiring of just the functions you need. You can also require everything if file size is not a concern (e.g. node environment).
// require everything
var color = require("pure-color");
// require all conversion functions
var convert = require("pure-color/convert");
color.convert === convert;
//require all parse functions
var parse = require("pure-color/parse");
color.parse === parse;
// require individual conversion function
var rgb2hsl = require("pure-color/convert/rgb2hsl");
// require individual parse function
var parseRgb = require("pure-color/parse/rgb");
The majority of conversion functions have the signature [Number] -> [Number]
. The exceptions are rgb2string
and rgb2hex
, whose signature is [Number] -> String
.
You can see all available conversions in the convert
directory.
var rgb2hsl = require("pure-color/convert/rgb2hsl");
var rgb2hex = require("pure-color/convert/rgb2hex");
rgb2hsl([255, 0, 0]); // [0, 100, 50]
rgb2hex([255, 0, 0]); // "#ff0000"
"pure-color/convert"
exports a hash of conversion functions keyed first by the "from" space, then by the "to" space:
var convert = require("pure-color/convert");
convert.rgb.hsl([1, 2, 3]);
convert["rgb"]["hsl"]([1, 2, 3]);
The conversion functions make no effort to handle alpha values. For instance:
// alpha value is lost in conversion...
rgb2hsl([255, 0, 0, 0.5]) // [0, 100, 50]
The reason for this is two-fold:
You must make effort to preserve alpha values between conversions yourself if this is important to you.
Any conversions that are simple compositions of other conversions have been omitted.
For example, let's imagine we wanted to convert hsl
to cmyk
. This function doesn't exist, but it can be trivially created by composing hsl2rgb
and rgb2cmyk
:
var hsl2rgb = require("pure-color/convert/hsl2rgb");
var rgb2cmyk = require("pure-color/convert/rgb2cmyk");
// define a new function composing the others
function hsl2cmyk(hsl) {
return rgb2cmyk(hsl2rgb(hsl));
}
// or use a higher-order compose function
var hsl2cmyk = compose(rgb2cmyk, hsl2rgb);
If there are missing conversions that cannot be achieved through composition, please raise an issue.
Parse functions have signature String -> [Number]
.
A generic parsing function is available, which accepts hsl
, rgb
, and hex
string formats. This always converts to rgb
space for consistency - if you don't know what format the color is to begin with, you don't know what color space will be returned.
var parse = require("pure-color/parse");
// parse is a function itself which converts hsl/rgb/hex string to `[r, g, b, a]`
parse("rgb(0, 0, 0)") // [0, 0, 0];
parse("hsl(0, 0, 0)") // [0, 0, 0];
parse("#000000") // [0, 0, 0];
// it also handles alpha
parse("rgba(0, 0, 0, 1)") // [0, 0, 0, 1];
parse("hsla(0, 0, 0, 1)") // [0, 0, 0];
Individual parsing functions are available if you know what format you will be parsing. Note that the hsl
parse function returns an hsl
array, whereas rgb
and hex
return an rgb
array
var parse = require("pure-color/parse");
var parseHsl = require("pure-color/parse/hex");
parseHsl("hsla(0, 0, 0, 1)") // [0, 0, 0, 1]
parse.hsl("hsla(0, 0, 0, 1)") // [0, 0, 0, 1]
No dependencies. Should work in any browser with ES5 support (which can be shimmed easily).
I have tried many color conversion/parsing libraries and I was not satisfied with any of them.
This library attempts to correct that by:
Contributions are welcome from everyone.
Issues can be resolved quickest if they are descriptive and include both a reduced test case and a set of steps to reproduce. Personal help requests filed as issues will be declined.
Please clearly explain the purpose of any pull request. In lieu of a formal style guide, please follow the current coding style. Tests would be nice, but are not essential.
Licensed under the MIT License
FAQs
Pure functions for color conversion and parsing
We found that pure-color demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.